#ifndef OAUTH_H
#define OAUTH_H

#include <QObject>
#include <QMap>
#include "ajax.h"

class OAuth : public QObject
{
    Q_OBJECT
public:
    explicit OAuth(QObject *parent = 0);
    QString hmacSha1(QByteArray key, QByteArray baseString);
    QString signature(const QString &method, const QString &url, const QString &query, const QString &key, const QString &tokenKey = "");
    QString signature(const QString &method, const QString &url, QMap<QString, QString> params, const QString &key, const QString &tokenKey = "");
    QMap<QString, QString> generateParameters(const QString &nonce = "", const QString &timestamp = "");
    QString generateSignature(const QString &method, const QString &url, const QString &nonce = "", const QString &timestamp = "");
    QString requestToken(const QString &method, const QString &url);
    QString requestAccessToken(const QString &method, const QString &url);
    QNetworkReply *requestApplication(const QString &method, const QString &url);
    QNetworkReply *requestApplication(const QString &method, const QString &url,
                               const QMap<QString, QString> &httpparam);

    QString consumerKey();
    void    setConsumerKey(QString key);

    QString consumerSecret();
    void    setConsumerSecret(QString secret);

    QString tokenKey();
    QString tokenSecret();

    void    setTokenVerifier(QString ver);

    QString appKey();
    void    setAppKey(const QString &appKey);
    QString appSecret();
    void    setAppSecret(const QString &appSecret);

signals:
    void performAuth(const QString &auth);
    void finishAccess();
    void appRequestFinish(const QUrl &url, const QString &data);
    void appRequestError(const QUrl &url, const QString &data);
    void appNetworkError();

public slots:
    void tokenFinished();
    void tokenError(QNetworkReply::NetworkError error);
    void accessFinished();
    void accessError(QNetworkReply::NetworkError error);
    void appFinished();
    void appError(QNetworkReply::NetworkError error);

private:
     QString _consumerKey;
     QString _consumerSecret;
     QString _tokenKey;
     QString _tokenSecret;
     QString _tokenVerifier;
     QString _appKey;
     QString _appSecret;
     Ajax    ajax;
};

#endif // OAUTH_H
